import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/data/request/home_request.dart';
import 'package:bxultimate/widgets/app/app_article_card.dart';
import 'package:bxultimate/widgets/mk_empty.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class TabItemView extends StatefulWidget {
  final String type;
  final String keywords;

  const TabItemView(
      {super.key, required this.type, required this.keywords});

  @override
  State<TabItemView> createState() => _TabItemViewState();
}

class _TabItemViewState extends State<TabItemView> {
  var listData = [];


  int page = 1;
  int size = 10;

  getData() async {
    listData.addAll( await HomeRequest.getSearchList(page, size, widget.keywords,widget.type));
    setState(() {
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {

    return EasyRefresh(
      header: TDRefreshHeader(),
      onRefresh: () async {
        listData = [];
        getData();
      },
      onLoad: () async {
        page++;
        getData();
      },
      emptyWidget: MkEmpty(),
      child: ListView.builder(
        itemCount: listData.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Get.toNamed(RouterName.articleDetail,arguments: listData[index].id);
            },
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
              child: AppArticleCard(
                article: listData[index],
              ),
            ),
          );
        },
      ),
    );
  }
}
